/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package privileges.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author JuanFelipe
 */
public class UserDAO {
    Connection conn;
    
    public UserDAO(Connection conn) {
        this.conn = conn;
    }    
    public ResultSet executeStatement( String sql ) {
        try {
            System.out.println(sql);
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery( sql );
            return resultSet;
        } catch (SQLException e) {
            return null;
        }
    } 
    public void dropUser(String name,String host) throws SQLException{
        Statement statement = conn.createStatement();
        statement.executeUpdate("DROP USER '" + name+"'@'"+host+"' ");
    }
    public void createUser(String name,String host,String password) throws SQLException{
        Statement statement = conn.createStatement();
        statement.executeUpdate("CREATE USER '"+name+"'@'"+host+"' IDENTIFIED BY '"+password+"'");
    }
    public void executeStatementUpdate(String sql) throws SQLException{
        System.out.println("sql recibido"+sql);
        Statement statement = conn.createStatement();
        statement.executeUpdate(sql);
    }

    public void changePassword(String user, String host, String password) throws SQLException {
        Statement statement = conn.createStatement();
        statement.executeUpdate("SET PASSWORD FOR '"+user+"'@'"+host+"' = PASSWORD('"+password+"');");
    }

    public ResultSet getTablesFromDB(String selectedSchema) {
        return executeStatement("SHOW TABLES FROM "+ selectedSchema+";");
    }

    public void changeUserHost(String user,String actualUser, String host,String actualHost) throws SQLException {
        Statement statement = conn.createStatement();
        statement.executeUpdate("RENAME USER '"+actualUser+"'@'"+actualHost+"' TO '"+user+"'@'"+host+"';");
    }
}
